﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace TextProject
{
	[XmlRootAttribute("formatted_text")]
	public class FormattedText
	{
		private List<CharacterDescriptor> charList = new List<CharacterDescriptor>();
        private List<Style> styles = new List<Style>();

		public FormattedText()
		{
		}

        public List<CharacterDescriptor> GetCharList()
        {
            return charList;
        }


		[XmlElement("cd")]
		public CharacterDescriptor [] CharList
		{
			get
			{
				return charList.ToArray();
			}
			set
			{
				charList.Clear();
				if (value != null)
					charList.AddRange(value);
			}
		}

		[XmlIgnore]
		public int Length
		{
			get
			{
				return charList.Count;
			}
		}

		[XmlIgnore]
		public CharacterDescriptor this [int idx]
		{
			get
			{
				return charList[idx];
			}
			set
			{
				charList[idx] = value;
			}
		}

        public Style GetStyle(int idx)
        {
            return styles[idx];
        }
        
		public void Insert(char c, Style style)
		{
            int styleIndex = styles.IndexOf(style);
            
            if (styleIndex < 0)
            {
                styleIndex = styles.Count;
                styles.Add(style);
            }

			charList.Add(new CharacterDescriptor(c, styles[styleIndex]));
		}
	}
}
